package controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import core.ProcessOwnerCore;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Department;
import model.Employee;
import org.jboss.logging.Logger;

/**
 *
 * @author phinguyen
 */
public class ServletAddDepartment extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @throws java.sql.SQLException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
       RequestDispatcher rd = null;
        HttpSession session = request.getSession();
        String namedepartment = request.getParameter("DeptName");
        String descriptiondepartment = request.getParameter("DeptDescription");
        System.out.println("vdgsegsdgfrsd");

        try {
            ProcessOwnerCore poc= new ProcessOwnerCore();
            String id_txt = request.getParameter("EmployeeDirector");
            System.out.println("EmployeeDirector : " + id_txt);
            Integer id = new Integer(id_txt);
            System.out.println(" id (integer) : " + id.toString());
            Employee directorEmp=new Employee();
            directorEmp= poc.getEmployee(id);
            Department dpt= new Department(namedepartment, descriptiondepartment, directorEmp);       
            poc.saveDepartmentobjet(dpt);
            rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);
        } catch (NumberFormatException ex) {
            System.out.println("null");
        }

    }
//    public static void main(String[] args){
//        for(int i=0; i<getAllEmployee().size();i++){
//            System.out.println(getAllEmployee().get(i).getEid());
//        }
//        System.out.println(getEmployee(3));
//    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ServletAddDepartment.class.getName()).log(Logger.Level.FATAL, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ServletAddDepartment.class.getName()).log(Logger.Level.FATAL, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
